package com.wwc.sc.controller;

import com.wwc.sc.pojo.Dept;
import com.wwc.sc.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * @author WWC
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/dept/addDept")
    public int addDept(Dept dept){
        dept.setDeptNo(String.valueOf(UUID.randomUUID()));
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/queryDept/{deptNo}")
    public Dept queryDept(@PathVariable("deptNo") String deptNo){
        return deptService.queryDept(deptNo);
    }

    @GetMapping("/dept/queryAllDept")
    public List<Dept> queryAllDept(){
        List<Dept> deptList = deptService.queryAllDept();
        return deptList;
    }

    @GetMapping("/dept/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        System.out.println("services====================================="+services);
        List<ServiceInstance> instances = discoveryClient.getInstances("SC-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.print(instance.getHost()+"\t");
            System.out.print(instance.getPort()+"\t");
            System.out.print(instance.getUri()+"\t");
            System.out.print(instance.getServiceId()+"\t");
            System.out.println();
        }
        return this.discoveryClient;
    }


}
